package cn.itcast.service.impl;


import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.mapper.PermissionMapper;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    PermissionMapper permissionMapper;


    /**
     *  权限设置-分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //PageHelper.startPage(当前页，每页显示条数)
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //将查询的结果封装为集合对象
        List<Permission> list = permissionMapper.findPage(queryPageBean.getQueryString());


        //将集合载入到PageInfo中，反推中总记录数
        PageInfo info = new PageInfo(list);

        //返回总页数，和list集合
        return new PageResult(info.getTotal(),list);
    }


    /**
     *  权限设置-新增权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {

        permissionMapper.add(permission);

    }


    /**
     * 权限设置-删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        Integer count = permissionMapper.findRoleIdAndPermissionId(id);

        if (count > 0){

            permissionMapper.deleteRoleId(id);

        }


        permissionMapper.deleteById(id);
    }


    /**
     *  权限设置-根据id查询(用于回显)
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {

        return permissionMapper.findById(id);

    }


    /**
     * 权限设置-修改
     * @param permission
     */
    @Override
    public void edit(Permission permission) {

        permissionMapper.edit(permission);
    }
}
